package com.FundamentalDemos.Function;

public class ParameterDemo {
    public static void main(String[] args) {
        //函数传参,形实结合,值传递与地址(特殊的值)传递
        int a = 10;
        test1(a);
        System.out.println(a);//10


        System.out.println("-------------------");
        int[] arr = new int[]{1, 2, 3};
        System.out.println("原来main中arr[1]=" + arr[1]);
        test2(arr);
        System.out.println("现在main中arr[1]=" + arr[1]);

        System.out.println("-------------------");
        arr = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (i + 1) * 10 + 1 + i;
        }
        printArray(arr);
        int[] arr1 = null;
        printArray(arr1);//空数组
        int[] arr2 = new int[]{};
        printArray(arr2);//零元素数组

        System.out.println("-------------------");
        arr1 = new int[]{1, 2, 3, 5, 5};
        arr2 = new int[]{1, 2, 3, 4, 5};
        System.out.println(isArrayEqual(arr1, arr2) ? "arr1 == arr2" : "arr1 != arr2");
        arr1 = null;
        arr2 = null;
        System.out.println(isArrayEqual(arr1, arr2) ? "arr1 == arr2" : "arr1 != arr2");
        arr1 = null;
        arr2 = new int[]{1, 2, 3};
        System.out.println(isArrayEqual(arr1, arr2) ? "arr1 == arr2" : "arr1 != arr2");


        System.out.println("-------------------");

    }

    public static void test1(int a) {
        System.out.println(a);//10
        a = 20;
        System.out.println(a);//20
    }

    public static void test2(int[] arr2) {//数组(引用类型)做形参
        System.out.println("原来test2中arr2[1]=" + arr2[1]);
        arr2[1] = 222;
        System.out.println("现在test2中arr2[1]=" + arr2[1]);
    }

    public static void printArray(int[] arr) {
        if (arr == null) {
            System.out.println("空指针异常!");
            return;
        }
        if (arr.length == 0) {
            System.out.println("数组中没有元素!");
            return;
        }
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");//是最后一项吗?是就不打印逗号,否则多打印个逗号
        }
        System.out.println("]");
    }

    /**
     * 判断两个整形数组是否相等
     *
     * @param arr1
     * @param arr2
     * @return boolean
     */
    public static boolean isArrayEqual(int[] arr1, int[] arr2) {
        boolean rs = true;//默认相等
        //层层拦截非法非法情况
        if (arr1 == null && arr2 == null) {//判空
            return rs;
        } else if (arr1 == null || arr2 == null) {
            rs = false;
            return rs;
        } else {
            if (arr1.length != arr2.length) {
                rs = false;
                return rs;
            }
            for (int i = 0; i < arr1.length; i++) {
                if (arr1[i] != arr2[i]) {
                    rs = false;
                    return rs;
                }
            }
        }
        return rs;
    }


}
